<?php
namespace app\admin\controller;
use think\exception\HttpResponseException;
use think\Response;
use \think\Exception;
abstract class Base extends \app\common\controller\AbsBase {
    
    protected $plat = 3;// 站点管理后台     
    protected $channel = 1; // web
    protected $role = 'site';  // 该平台登录的角色
    public function __construct(){
        parent::__construct();
        $this->assign([
            'htmlViewPath'  => '../application/admin/view', // 模板view目录路径
            'htmlStaticPath'=> '/static/admin',
            'htmlVersion'   => 6
        ]);
    }
    
    /**
     * 获取访问的节点
     */
    protected function getAccessNode(){
        return strtolower('/'.request()->controller().'/'.request()->action());
    }
    
    /**
     * 返回给抽象类登录平台的信息
     */
    protected function getLoginPlat(){
        return $this->plat;
    }
    
    /**
     * 返回入口渠道
     */
    protected function getLoginChannel(){
        return $this->channel;
    }
    
    
    /**
     * 成功提示
     */
    protected function success($msg = '', $url = null, $data = '', $wait = 3, array $header = []){
        if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
            $url = $_SERVER["HTTP_REFERER"];
        } elseif ('' !== $url) {
            $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : Container::get('url')->build($url);
        }
        $result = [
            'code' => 1,
            'msg'  => $msg,
            'data' => $data,
            'url'  => $url,
            'wait' => $wait,
        ];

        $type = $this->getResponseType();
        // 把跳转模板的渲染下沉，这样在 response_send 行为里通过getData()获得的数据是一致性的格式
        if ('html' == strtolower($type)) {
            $type = 'jump';
        }

        $response = Response::create($result, $type)->header($header)->options(['jump_template' => "public/success"]);
        throw new HttpResponseException($response);
    }

    /**
     * 操作错误跳转的快捷方法
     * @access protected
     * @param  mixed     $msg 提示信息
     * @param  string    $url 跳转的URL地址
     * @param  mixed     $data 返回的数据
     * @param  integer   $wait 跳转等待时间
     * @param  array     $header 发送的Header信息
     * @return void
     */
    protected function error($msg = '', $url = null, $data = '', $wait = 3, array $header = []){
        $type = $this->getResponseType();
        if (is_null($url)) {
            $url = $this->app['request']->isAjax() ? '' : 'javascript:history.back(-1);';
        } elseif ('' !== $url) {
            $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app['url']->build($url);
        }

        $result = [
            'code' => 0,
            'msg'  => $msg,
            'data' => $data,
            'url'  => $url,
            'wait' => $wait,
        ];

        if ('html' == strtolower($type)) {
            $type = 'jump';
        }

        $response = Response::create($result, $type)->header($header)->options(['jump_template' => "public/error"]);
        throw new HttpResponseException($response);
    }

}
